﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using BingZi.HY.Core.Entities.Center;
using NewLife.Web;
using BingZi.HY.Core.Entities.System;
using BingZi.HY.Core.AsyncExecute;
using BingZi.HY.Core.Enums;




namespace FrameWork.web.Includes.ET
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class etpost : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            //商业信息
            app_BusinessInfo app = new app_BusinessInfo();


            HttpContext.Current.Response.ContentType = "text/plain";
            HttpContext.Current.Response.Buffer = true;
            //HttpContext.Current.Session.LCID = 2052; //中国地区LCID
            HttpContext.Current.Response.Charset = "utf-8"; //设置入库编码


            //首先验证标题和用户名是否同时存在
            if (getForm("Title") != string.Empty && getForm("UserName") != string.Empty)
            {
                app.b_Title = getForm("Title");//获取标题
                app.b_UserName = getForm("UserName").Trim();//获取用户名
                app.b_CompanyName = getForm("CompanyName");//获取公司名称




                if (app_BusinessInfo.FindCount(new string[] { app_BusinessInfo._.b_Title, app_BusinessInfo._.b_UserName }, new string[] { app.b_Title, app.b_UserName }) == 0)
                {
                    //该信息对应的用户信息不存在,进行下一步动作




                    //验证用户是否已注册,若注册返回该用户的编号已经公司等信息
                    UserInfo sys_User = UserInfo.FindByName(app.b_UserName);
                    if (sys_User == null || sys_User.ID <= 0)
                    {
                        sys_User = new UserInfo();
                        //构造用户之前先构造用户的密码
                        string UserPassWrod = "888888";

                        #region "构造用户信息"


                        sys_User.Password = NewLife.Security.DataHelper.Hash(UserPassWrod);

                        sys_User.Name = app.b_UserName;
                        sys_User.DisplayName = getForm(UserInfo._.Name);//姓名
                        sys_User.LastLogin = DateTime.Now;
                        sys_User.LastLoginIP = WebHelper.UserHost;

                        sys_User.Role = MemberLevel.Find(MemberLevel._.Name, "普通会员"); //设置为普通用户
                        sys_User.Email = getForm(UserInfo._.Email);//获取邮件地址
                        sys_User.IsEnable = true;
                        sys_User.Phone = getForm(UserInfo._.Phone);
                        sys_User.MobilePhone = getForm(UserInfo._.MobilePhone);
                        sys_User.Fax = getForm(UserInfo._.Fax);


                        //获取性别
                        string Sex = getForm(UserInfo._.Sex);
                        if (Sex.Contains("女士"))
                            sys_User.Sex = 2;
                        else if (Sex.Contains("先生"))
                            sys_User.Sex = 1;
                        else
                            sys_User.Sex = 0;


                        #endregion



                        //创建用户信息
                        sys_User.ID = sys_User.Insert();
                        if (sys_User.ID <= 0)
                        {
                            setResponseWrite(string.Format("[no]用户:{0},信息注册失败！", sys_User.Name));
                        }



                        //验证公司信息是否存在
                        app_Company Company = app_Company.Find(app_Company._.UserName, app.b_UserName);
                        if (Company == null || Company.ValueID <= 0)
                        {
                            Company = new app_Company();
                            #region "构造公司信息"

                            Company.UserID = sys_User.ID;
                            Company.UserName = sys_User.Name;
                            Company.c_HomePage = getForm(app_Company._.c_HomePage);
                            Company.c_Address = getForm(app_Company._.c_Address);
                            Company.U_Email = sys_User.Email;
                            Company.U_Fax = sys_User.Fax;
                            Company.U_MobilePhone = sys_User.MobilePhone;
                            Company.c_Name = app.b_CompanyName;
                            Company.U_Phone = sys_User.Phone;
                            Company.U_Position = sys_User.Position;
                            Company.c_State = (int)EnumState.Normal;
                            Company.c_LastIP = sys_User.LastLoginIP;


                            app_CompanyType CompanyType = app_CompanyType.FindByName(getForm(app_Company._.c_Type));
                            Company.c_Type = CompanyType != null ? CompanyType.CT_ID : 0;

                            #endregion


                            //创建公司信息
                            if (Company.Insert() > 0 && Company.ValueID > 0)
                            {
                                #region "构造邮件并发送邮件"
                                BingZi.HY.Core.MailEngine.Message msg = new BingZi.HY.Core.MailEngine.Message();
                                //替换邮件模板
                                msg.Mail = sys_User.Email; //getForm("U_Email");
                                msg.Name = app.b_UserName;
                                msg.Subject = string.Format("{0} 您好,贵公司{1} 已经在百分百商务网注册成功!", app.b_UserName, app.b_CompanyName);
                                msg.Body = app_EmailTemplate.GetContentByKey(EnumMailType.FetchUser.ToString());
                                if (msg.Body != string.Empty)
                                {



                                }
                                else
                                {
                                    msg.Body = msg.Subject;
                                }

                                //异步发送邮件
                                Processor.AssignMessage(msg);
                                setResponseWrite("[ok]");


                                #endregion
                            }
                            else
                            {
                                setResponseWrite(string.Format("[no]公司:{0} 注册失败！", Company.c_Name));
                            }

                        }


                    }




                    #region "构造待入库的信息"

                    app_BusinessInfo bInfo = new app_BusinessInfo();
                    //构造入库的信息




                    //保存入库的信息
                    bInfo.b_Id = bInfo.Insert();
                    if (bInfo.b_Id > 0)
                    {
                        //入库成功

                        //建立索引信息
                        //    Simple.AddDocument(app);



                        setResponseWrite("[ok]");
                    }







                    #endregion












                }


                //    //数据库验证是否存在
                //    if (BusinessFacadeFrameSite.app_BusinessInfoListCount(app.b_UserName, app.b_Title) == 0)
                //    {
                //        //该信息对应的用户信息不存在,进行下一步动作

                //        //验证用户是否已注册,若注册返回该用户的编号已经公司等信息
                //        sys_UserTable sys_User = BusinessFacade.sys_UserDisp(Common.inSQL(app.b_UserName));

                //        if (sys_User.UserID == 0)
                //        {
                //            //该用户现在未注册,需要先注册


                //            //随机生成密码
                //            sys_User.U_Password = Common.md5( Common.RndNum(8),32);
                //            sys_User.U_LoginName = app.b_UserName;
                //            sys_User.U_CName = getForm("U_CName");//姓名
                //            sys_User.U_LastDateTime = DateTime.Now;
                //            sys_User.U_LastIP = Common.GetIPAddress();
                //            sys_User.U_DateTime = DateTime.Now;
                //            sys_User.DB_Option_Action_ = "Insert";
                //            sys_User.U_Type = 1; //设置为普通用户
                //            sys_User.U_Email = getForm("U_Email");//获取邮件地址
                //            sys_User.U_Status = 0;

                //            //获取性别
                //            if(getForm("U_Sex").Contains("女士"))
                //                sys_User.U_Sex = 0;
                //            else
                //                sys_User.U_Sex = 1;

                //            //将会员信息入库
                //            int rInt = BusinessFacade.sys_UserInsertUpdate(sys_User);
                //            if (rInt != 0)
                //            {
                //                //用户注册成功
                //                sys_User.UserID = rInt;

                //                //创建公司库信息
                //                app_CompanyEntity app_Company = new app_CompanyEntity();
                //                app_Company.UserID = sys_User.UserID;
                //                app_Company.U_LoginName = sys_User.U_LoginName;
                //                app_Company.c_HomePage = getForm("c_HomePage");
                //                app_Company.c_Address = getForm("c_Address");
                //                app_Company.c_Email = sys_User.U_Email;
                //                app_Company.c_Fax = getForm("c_Fax");
                //                app_Company.c_Mobile = getForm("c_Mobile");
                //                app_Company.c_Name = app.b_CompanyName;
                //                app_Company.c_Phone = getForm("c_Phone");
                //                app_Company.c_Position = getForm("c_Position");
                //                app_Company.c_State = getInfoState(getForm("InfoState"));
                //                app_Company.c_IP = sys_User.U_LastIP;

                //                //公司类型
                //                switch (getForm("c_Type"))
                //                {
                //                    case "生产型": app_Company.c_Type = ((int)EnumCompanyType.Produce).ToString(); break;
                //                    case "贸易型": app_Company.c_Type = ((int)EnumCompanyType.Sell).ToString(); break;
                //                    case "服务型": app_Company.c_Type = ((int)EnumCompanyType.Serve).ToString(); break;
                //                    default: app_Company.c_Type = ((int)EnumCompanyType.Other).ToString(); break;
                //                }

                //                if (BusinessFacadeFrameSite.app_CompanyInsertUpdateDelete(app_Company) <= 0)
                //                {
                //                    setResponseWrite(string.Format("[no]公司:{0} 注册失败！", app_Company.c_Name));
                //                }
                //            }
                //            else
                //            {
                //                setResponseWrite(string.Format("[no]用户:{0},信息注册失败！", sys_User.U_LoginName));
                //            }

                //        }

                //        //开始入库商业信息资料
                //        app.b_UserID = sys_User.UserID;
                //        app.b_Content = getForm("content");
                //        app.b_InfoPhoto = getForm("b_InfoPhoto");
                //        app.b_Tags = getForm("b_Tags");
                //        app.b_AreaIds = "";
                //        app.b_AreaNames = "";
                //        app.b_ShopCategory = 0;
                //        app.b_Category = 0;
                //        app.b_CategoryText = "";
                //        app.b_Balance = 100;    //默认权重分值
                //        app.b_EffectTime = 30;  //信息有效时间(天)
                //        app.b_InfoState = getInfoState(getForm("InfoState"));
                //        app.b_InfoType = getInfoType(getForm("InfoType"));
                //        app.b_MaxUnit = getForm("b_MaxUnit");
                //        app.b_MinUnit = getForm("b_MinUnit");
                //        app.b_Unit = getForm("b_Unit");
                //        string sb_Price = getForm("b_Price");
                //        if (Validator.IsNumber(sb_Price))
                //            app.b_Price = Convert.ToDouble(sb_Price);
                //        else
                //            app.b_Price = 0;

                //        app.b_StartTime = DateTime.Now;
                //        app.b_UpdateTime = DateTime.Now;
                //        app.DataTable_Action_ = DataTable_Action.Insert;

                //        for (int i = 0; i < 20; i++)
                //            BusinessFacadeFrameSite.app_BusinessInfoQueue(app);


                //        setResponseWrite("[ok]");

                //int rBI_ID = BusinessFacadeFrameSite.app_BusinessInfoInsertUpdateDelete(app);
                //if (rBI_ID != 0)
                //{
                //    app.b_Id = rBI_ID;

                //    //建立索引信息
                //    Simple.AddDocument(app);

                //    //调用发送邮件


                //    setResponseWrite("[ok]");
                //}
                //else
                //    setResponseWrite("信息写入数据库失败！");


                //}
                //else
                //{
                //    //已经同时在数据库中存在，后续内容部入库,程序将返回
                //    setResponseWrite(string.Format("[no]信息:{0},对应用户:{1}信息已经存在,不予入库", app.b_Title, app.b_UserName));
                //}

            }

        }

        /// <summary>
        /// 获取Form传值
        /// </summary>
        /// <param name="FormKey">Form的参数</param>
        /// <param name="DefaultValue">默认值</param>
        /// <returns></returns>
        public string getForm(string FormKey, string DefaultValue)
        {
            return string.IsNullOrEmpty(HttpContext.Current.Request.Form[FormKey]) ? DefaultValue : HttpContext.Current.Request.Form[FormKey].Trim();
        }
        /// <summary>
        /// 获取Form传值,空值为string.Empty
        /// </summary>
        ///  <param name="FormKey">Form的值</param>
        /// <returns></returns>
        public string getForm(string FormKey)
        {
            return getForm(FormKey, string.Empty);
        }

        /// <summary>
        /// 设置要返回的消息,并终止当前程序执行
        /// </summary>
        /// <param name="Msg"></param>
        public void setResponseWrite(string Msg)
        {
            HttpContext.Current.Response.Write(Msg);
            HttpContext.Current.Response.End();
        }

        /// <summary>
        /// 根据信息类型的名字判断该类型的枚举值
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public string getInfoType(string name)
        {
            int InfoType = 0;
            //switch (name)
            //{
            //    case "供应": InfoType = (int)EnumInfoType.Sale; break;
            //    case "求购": InfoType = (int)EnumInfoType.Buy; break;
            //    case "合作": InfoType = (int)EnumInfoType.Cooperation; break;
            //    case "展会": InfoType = (int)EnumInfoType.Expo; break;
            //    case "租赁": InfoType = (int)EnumInfoType.Lease; break;
            //    case "二手": InfoType = (int)EnumInfoType.Old; break;
            //    case "加工": InfoType = (int)EnumInfoType.Process; break;
            //    case "代理": InfoType = (int)EnumInfoType.Proxy; break;
            //    case "库存": InfoType = (int)EnumInfoType.Reserve; break;
            //    default: InfoType = (int)EnumInfoType.Sale; break;
            //}
            return InfoType.ToString();
        }


        public string getInfoState(string name)
        {
            int InfoType = 0;
            //switch (name)
            //{
            //    case "未审核": InfoType = (int)EnumAuditState.ForStart; break;
            //    case "已审核": InfoType = (int)EnumAuditState.ForNormal; break;
            //    case "已锁定": InfoType = (int)EnumAuditState.ForLock; break;
            //    case "已过期": InfoType = (int)EnumAuditState.ForOverdue; break;
            //    case "未通过": InfoType = (int)EnumAuditState.ForUnPass; break;
            //    default: InfoType = (int)EnumAuditState.ForStart; break;


            //}
            return InfoType.ToString();
        }



        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
